package com.egg.ylt.presenter.impl;

import com.egg.ylt.Utils.API;
import com.egg.ylt.presenter.INewPhonePresenter;
import com.egg.ylt.view.INewPhoneView;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.CommonUtils;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.net.ReqCallBack;
import com.yonyou.framework.library.net.RequestManager;

import java.util.HashMap;

/**
 * Created by sichaoqi on 2018/10/15.
 */

public class NewPhonePresenterImpl extends BasePresenter<INewPhoneView> implements INewPhonePresenter {

    @Override
    public void getCode(String phone) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("phone", phone);
        RequestManager.getInstance().requestPostByAsyn(API.GET_PHONE_CODE, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
//                mView.showError(result);
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
//                mView.showException(error.toString());
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }

    @Override
    public void savePhone(String phone, String companyId, String token, String verificationCode) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("phone", phone);
        hashMap.put("companyId", companyId);
        hashMap.put("token", token);
        hashMap.put("verificationCode", verificationCode);
        RequestManager.getInstance().requestPostByAsyn(API.GET_UPDATE_PHONE, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.saveNewPhone();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }

    @Override
    public void checkedPhone(String phone, String companyId, String type) {
        mView.showDialogLoading("数据加载中");
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("phone", phone);
        hashMap.put("companyId", companyId);
        hashMap.put("type", type);
        RequestManager.getInstance().requestPostByAsyn(API.GET_USER_PHONE_PRUE, hashMap, new ReqCallBack<String>() {

            @Override
            public void onReqSuccess(String result) {
                mView.dismissDialogLoading();
                mView.getPhoneCode();
            }

            @Override
            public void onFailure(String result) {
                mView.dismissDialogLoading();
                mView.showError(result);
                CommonUtils.makeEventToast(mContext, result, false);
            }

            @Override
            public void onReqFailed(ErrorBean error) {
                mView.dismissDialogLoading();
//                mView.showException(error.toString());
                CommonUtils.makeEventToast(mContext, error.getMsg(), false);
            }
        });
    }
}
